package com.wnxy.sddfp.system.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wnxy.sddfp.common.exception.Assert;
import com.wnxy.sddfp.common.model.dto.bank.CardBindDto;
import com.wnxy.sddfp.common.model.dto.bank.SignatureDto;
import com.wnxy.sddfp.common.model.dto.user.LoginDto;
import com.wnxy.sddfp.common.model.dto.user.UserRegisterDto;
import com.wnxy.sddfp.common.model.vo.LoginVo;
import com.wnxy.sddfp.common.model.vo.UserInfoVo;
import com.wnxy.sddfp.common.result.Result;
import com.wnxy.sddfp.common.result.enums.impl.BusinessCode;
import com.wnxy.sddfp.system.entity.User;
import com.wnxy.sddfp.system.entity.UserDetail;
import com.wnxy.sddfp.system.service.IUserDetailService;
import com.wnxy.sddfp.system.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author yellow docker
 * @since 2024-04-12
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @Autowired
    private IUserDetailService userDetailService;

    @GetMapping("test")
    public String hello(){
        return "hello";
    }

    @GetMapping("test2")
    public Result hello2(){
        return Result.ok("测试成功。。。。");
    }

    @GetMapping("test3")
    public Result hello3(){
//        if(true){
//            //余额不足
//            // throw new BusinessException(BusinessCode.BALANCE_NOT_ENOUGH);
//            //抛出异常的封装
//            Assert.error(BusinessCode.BALANCE_NOT_ENOUGH);
//        }
        return Result.ok("测试成功。。。。");
    }

    /**
     * 注册
     * @param userRegisterDto
     * @return
     */
    @PostMapping("register")
    public Result register(@RequestBody @Valid UserRegisterDto userRegisterDto){
        System.out.println("userRegisterDto = " + userRegisterDto);

        userService.register(userRegisterDto);

        return Result.ok("注册成功");
    }

    /**
     *  登录实现
     * @param loginDto
     * @return
     */
    @PostMapping("login")
    public Result login(@RequestBody LoginDto loginDto){
        System.out.println("loginDto = " + loginDto);
        //登录
        LoginVo loginVo = userService.login(loginDto);
        return Result.ok(loginVo);
    }

    /**
     * 银行卡进行绑卡
     *    绑卡的时候(用户已经登录)
     *       昨天有token (客户端中header 需要携带token) 访问这个接口需要带token
     * @return
     */
    @PostMapping("/bank/card/add")
    public Result addBankAccount(@RequestBody CardBindDto cardBindDto){

//        //获取userId   HttpServletRequest \ @RequestHeader 注解   如果我在很多方法中获取
//        String authorization = request.getHeader("Authorization");
//        System.out.println("authorization = " + authorization);
//        String token = authorization.replace(" Bearer ", "");
//        System.out.println("token = " + token);

        //绑卡的实现
        SignatureDto signatureDto = userService.addBankAccount(cardBindDto);
        return Result.ok(signatureDto);
    }

    @GetMapping("find/info/{userId}")
    public UserInfoVo findUserById(@PathVariable("userId") String userId){


        UserInfoVo userInfoVo = new UserInfoVo();
        //通过id查询
        User user = userService.getById(userId);

        userInfoVo.setUserId(userId);
        //user 表中
        userInfoVo.setUserName(user.getUserName());
        userInfoVo.setIdentityCode(user.getIdentityCode());

        //user_detail 表中
        UserDetail userDetail = userDetailService.getById(userId);
        userInfoVo.setUserCode(userDetail.getUserCode());

        return userInfoVo;
    }
}
